package com.ihrm.audit.controller;


import com.ihrm.audit.entity.ProcInstance;
import com.ihrm.audit.entity.ProcTaskInstance;
import com.ihrm.audit.service.AuditService;
import com.ihrm.audit.service.ProcessService;
import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/user/process")
public class ProcessController extends BaseController {
    
    @Autowired
    private ProcessService processService;
    
    @Autowired
    private AuditService auditService;
    
    /**
     * 部署新流程
     *      前端将绘制好的流程模型图(bpmn)文件上传到方法中
     * @param file 上传的文件
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/deploy",method = RequestMethod.POST)
    public Result deployProcess(@RequestParam("file")MultipartFile file) throws IOException{
        processService.deployProcess(file,companyId);
        return  new Result(ResultCode.SUCCESS);
    }
    
    /**
     * 查询所有的流程定义
     */
    @RequestMapping(value = "/definition",method = RequestMethod.GET)
    public Result definitionList() throws IOException{
        List list = processService.getProcessDefinitionList(companyId);
        return new Result(ResultCode.SUCCESS,list);
    }
    
    /**
     * 设置流程的挂起与激活状态
     */
    @RequestMapping(value = "/suspend/{processKey}",method = RequestMethod.GET)
    public Result suspendProcess(@PathVariable String processKey) throws IOException{
        processService.suspendProcess(processKey,companyId);
        return new Result(ResultCode.SUCCESS);
    }
    
    
    /**
     * 查询申请列表
     */
    @RequestMapping(value = "/instance/{page}/{size}",method = RequestMethod.PUT)
    public Result instanceList(@RequestBody ProcInstance instance,@PathVariable int page,
                               @PathVariable int size) throws IOException{
        Page pages = auditService.getInstanceList(instance, page, size);
        PageResult pr = new PageResult(pages.getTotalElements(), pages.getContent());
        return new Result(ResultCode.SUCCESS,pr);
    }
    
    /**
     * 查询申请的详情数据
     * @param id 申请对象的id
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/instance/{id}",method = RequestMethod.GET)
    public Result instanceDetail(@PathVariable String id) throws IOException{
        ProcInstance instance = auditService.findInstanceDetail(id);
        return new Result(ResultCode.SUCCESS,instance);
    }
    
    /**
     * 流程申请
     */
    @RequestMapping(value = "/startProcess",method = RequestMethod.POST)
    public Result startProcess(@RequestBody Map map) throws IOException{
        auditService.startProcess(map,companyId);
        return new Result(ResultCode.SUCCESS);
    }
    
    /**
     * 提交审核
     *      handleType 处理类型
     *          2审批通过；3审批不通过；4撤销
     */
    public Result commit(@RequestBody ProcTaskInstance taskInstance) throws IOException{
        auditService.commit(taskInstance,companyId);
        return new Result(ResultCode.SUCCESS);
    }
    
    /**
     * 查询流程任务明细
     */
    @RequestMapping(value = "/instance/tasks/{id}",method = RequestMethod.GET)
    public Result tasks(@PathVariable String id)throws IOException{
        return new Result(ResultCode.SUCCESS,auditService.findTasksByProcess(id));
    }

}
